package com.quasar.hpatient.widget;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.text.TextWatcher;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.bigkoo.pickerview.OptionsPickerView;
import com.quasar.hpatient.R;
import com.quasar.hpatient.model.custommodel.DetectionData;
import com.quasar.hpatient.utils.LogAndToastUtil;
import com.quasar.hpatient.view.adapter.TestrecordsItemAdapter;
import com.vise.log.ViseLog;

import java.util.ArrayList;
import java.util.List;


public class WidgetProteinNumber extends LinearLayout implements View.OnClickListener {

    public interface OnSave {
        public void save(WidgetProteinNumber item);
    }

    public OnSave onSave;


    public WidgetProteinNumber(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context, attrs, defStyleAttr);
    }

    public WidgetProteinNumber(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context, attrs, 0);
    }

    private float number;
    private TextView minus, plus;
    private EditText value;
    Context context;

    public void init(Context context, AttributeSet attrs, int defStyleAttr) {
        LayoutInflater.from(context).inflate(R.layout.widget_number, this);
        value = (EditText) findViewById(R.id.value);
        value.setFocusable(false);
        minus = (TextView) findViewById(R.id.minus);
        plus = (TextView) findViewById(R.id.plus);
        final TypedArray a = getContext().obtainStyledAttributes(
                attrs, R.styleable.WidgetNumber);
        number = a.getInt(R.styleable.WidgetNumber_number_value, 0);
        a.recycle();
        //setClickable(true);
        //this.setOnClickListener(this);
        minus.setOnClickListener(this);
        plus.setOnClickListener(this);
        value.setOnClickListener(this);
        value.setText("0.0");
        this.context=context;
        minus.setVisibility(View.GONE);
        plus.setVisibility(View.GONE);
        setpickView();
    }

    @Override
    public void setOnClickListener(OnClickListener l) {
        //super.setOnClickListener(l);
        minus.setOnClickListener(l);
        plus.setOnClickListener(l);
    }

    public void setNumber(float num) {
        number = num;
        value.setText(num + "");
    }

    public float getNumber() {
        float result = 0;
        try {
            result = Float.valueOf(value.getText().toString());
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

    @Override
    public void onClick(View view) {


        StringBuffer stringBuffer=new StringBuffer();
        stringBuffer.append("阴性(-)");
        stringBuffer.append("微量(+-)");
        stringBuffer.append("1+(+)");
        stringBuffer.append("2+(++)");
        stringBuffer.append("3+(+++)");
        stringBuffer.append("4+(++++)");


        if(view.getId()==R.id.value){
            pickView();
//            LogAndToastUtil.toast(context,stringBuffer.toString(),"");
        }
        if (view == minus) {
            if (number > 1.1) {
                number = number - 1;
                value.setText(number + "");
            } else {
                if (number > 0f) {
                    number = number - 0.5f;
                    value.setText(number + "");
                } else {
                    number = -1f;
                    value.setText(number + "");
                }
            }
        } else if (view == plus) {

            if(number<0.0){
                number = 0.0f;
                value.setText(0.0f + "");
            }else if (number>0.0 && number < 0.9) {
                number = number + 0.5f;
                value.setText(number + "");
            } else {
                if (number > 3.9) {

                } else {
                    number = number + 1f;
                    value.setText(number + "");
                }
            }

        }
        if (onSave != null) {
            onSave.save(this);
        }

    }

    public void setLetfTextFwatch(TextWatcher textFwatch) {
        value.addTextChangedListener(textFwatch);
    }

    List<DetectionData> detectionDataList=null;
    //赋值
    public void setpickView(){
        detectionDataList=new ArrayList<>();
        detectionDataList.add(new DetectionData("(阴性)- -1",-1f));
        detectionDataList.add(new DetectionData("(阳性)微量或者+- 0.5",0.5f));
        detectionDataList.add(new DetectionData("(阳性)1+或者+ 1",1f));
        detectionDataList.add(new DetectionData("(阳性)2+或者++ 2",2f));
        detectionDataList.add(new DetectionData("(阳性)3+或者+++ 3",3f));
        detectionDataList.add(new DetectionData("(阳性)4+或者++++ 4",4f));

    }
    //更换显示方式
    public void pickView(){
        OptionsPickerView pvOptions = new OptionsPickerView.Builder(context, new OptionsPickerView.OnOptionsSelectListener() {
            @Override
            public void onOptionsSelect(final int options1, int options2, int options3, View v) {
                //返回的分别是三个级别的选中位置
                final String tx = detectionDataList.get(options1).getPickerViewText();
                ViseLog.d("选择的规格"+tx);
                value.setText(detectionDataList.get(options1).getValues()+"");
            }
        })

                .setTitleText("检测值")
                .setDividerColor(Color.BLACK)
                .setTextColorCenter(Color.BLACK) //设置选中项文字颜色
                .setContentTextSize(20)
                .setOutSideCancelable(false)// default is true
                .build();
        pvOptions.setPicker(detectionDataList);//三级选择器
        pvOptions.show();
    }
}